<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>转换为 FASTA 格式</title>
    <style>
        textarea.fasta {
            margin-top: 10px;
        }
        div.box {
            margin: 5px;
            padding: 5px;
            border: 1px solid gray;
        }
    </style>
</head>

<body>
    <h1>简单文本处理，获取 FASTA 格式。</h1>
    <div id="tab-to-fasta" class="box">
        <h2>TAB 格式与 FASTA 格式互相转换。</h2>
        <p>只接受每行一个 id、一条序列。</p>
        <textarea class="tab" cols="100" rows="20"></textarea><br>
        <button onclick="tab_to_fasta('tab-to-fasta')">TAB to FASTA</button>&emsp;&emsp;
        <button onclick="fasta_to_tab('tab-to-fasta')">FASTA to TAB</button><br>
        <textarea class="fasta" cols="100" rows="20"></textarea>
    </div>
    <script>
        function tab_to_fasta(id) {
            var raw = document.querySelector('#'+id+' textarea.tab').value;
            if (raw.match(/^\s*$/)) return;
            var content = '';
            raw.split('\n').forEach(elem => {
                if (elem.match(/^\s*$/)) return;
                var arr = elem.split('\t');
                var sid = arr[0], seq = arr[1];
                content += '>' + sid + '\n' + seq + '\n';
            });
            document.querySelector('#'+id+' textarea.fasta').value = content;
        }

        function fasta_to_tab(id) {
            var raw = document.querySelector('#'+id+' textarea.fasta').value;
            if (raw.match(/^\s*$/)) return;
            var content = '';
            raw.split('>').forEach(elem => {
                if (elem.match(/^\s*$/)) return;
                var arr = elem.split('\n');
                var sid = arr.shift(), seq = arr.join('');
                content += sid + '\t' + seq + '\n';
            });
            document.querySelector('#'+id+' textarea.tab').value = content;
        }
    </script>
</body>

</html>